<?php
class PublicController extends Zendvn_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	protected $_namespace;
	
	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';	

							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		/*$this->_langObj->setLangFile(array('default.language.tmx'));		
		Zend_Registry::set('Zend_Translate', $this->_langObj->generate());*/
		
		$info = new Zendvn_System_Info();
		
		$language = $info->getLanguage('admin');
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
		$template_path = TEMPLATE_PATH . "/login/system";
		$this->loadTemplate($template_path,'template.ini','template');
	}
	
	public function errorAction()
	{
		$this->view->Title = 'Message: Error!';
		$this->view->headTitle($this->view->Title,true);
		$error[] = 'Chuc nang nay khong ton tai.';
		$this->view->messageError = $error;
	}
	public function noAccessAction()
	{
		$this->view->Title = 'No Access';
		$this->view->headTitle($this->view->Title,true);
		$error[] = 'Ban khong quyen truy cap vao chuc nang nay.';
		$this->view->messageError = $error;
		$this->_helper->viewRenderer('error');
		$this->_redirect('/default/index/index/');
	}
	
	public function loginAction()
	{ 
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
		$this->view->Title = 'Login';
		$this->view->headTitle($this->view->Title,true);		
		if($this->_request->isPost()){
			$yourCart = new Zend_Session_Namespace('cart');			
			$yourCart->unsetAll();
			$validator = new Default_Form_ValidateLogin($this->_arrParam);
			if($validator->isError() == true)
			{
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
                                $a=$validator->getData();
			}else
			{
				$auth = new Zendvn_System_Auth();
				if($auth->login($this->_arrParam))
				{				
					$info = new Zendvn_System_Info();
					$info->createInfo();
					$this->_redirect('/default/admin/index/');
				}else{
					$error[] = $auth->getError();
					$this->view->messageError = $error;
				}
			}
		}
		
	}	
	public function logoutAction()
	{
		$this->view->Title = 'Logout';
		$this->view->headTitle($this->view->Title,true);
		$auth = new Zendvn_System_Auth();
		$auth->logout();
		
		$info = new Zendvn_System_Info();
		$info->destroyInfo();
		
		$link = $this->view->baseUrl('/default/index/index');
		$this->view->Notes = 'Ban da thoat he thong.
							<a style="color:#dafa03;text-decoration: underline" href="' . $link . '">Nhan vao day</a> de qua lai trang chu
						';
		
		//$this->_helper->viewRenderer('error');
	}
	
	public function registerAction(){
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
		$this->view->Title = 'Đăng ký thành viên';
		$this->view->headTitle($this->view->Title,true);
		$captcha = new Zendvn_Captcha_Image();
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
		if($this->_request->isPost()){
			$captchaID = $this->_arrParam['captchaID'];
			$validator = new Default_Form_ValidateRegister($this->_arrParam);
			if($validator->isError() == true){				
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			}else{
				$arrParam = $validator->getData(array('upload'=>true));
				$tblUser 	= new Zendvn_Models_Users();
				$tblUser->saveItem($arrParam,array('task'=>'user-register'));
				$this->_redirect('/default/public/notice');
			}
			$captcha->removeImg($captchaID);
		}
		$captcha->removeImg($this->view->captcha_id,array('exception'=>true));
	}
	public function noticeAction(){
			$template_path = TEMPLATE_PATH . "/public/system";
			$this->loadTemplate($template_path,'template.ini','template');
	}
}




